//
//  NewsCell.m
//  SimulationTechnologyHeadlines
//
//  Created by 移云 on 2019/10/18.
//  Copyright © 2019 admin. All rights reserved.
//

#import "NewsCell.h"
#import "News.h"
#define fontSize 13  //字体大小
@interface NewsCell()

@property(nonatomic,weak)UILabel *titleLabel;//标题
@property(nonatomic,weak)UILabel *contentLabel;//摘要
@property(nonatomic,weak)UILabel *readnumAndLikenumLabel;//阅读和喜欢的人数
@property(nonatomic,weak)UILabel *nicknameLabel;//发布平台的名称
@property(nonatomic,weak)UIImageView *picImageView;//配图

@end

@implementation NewsCell

+ (instancetype)newsCellWithTableView:(UITableView *)tableView
{
    NSString *ID = @"newsCell";
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[NewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}



- (void)setNewsModel:(News *)newsModel
{
    _newsModel = newsModel;
    self.titleLabel.text =  newsModel.title;
    self.contentLabel.text = newsModel.content;
    self.nicknameLabel.text = newsModel.nickname;

    NSURL *picURL = [NSURL URLWithString:newsModel.picurl];
    NSData *picData = [NSData  dataWithContentsOfURL:picURL];
    UIImage *image = [UIImage imageWithData:picData];
    self.picImageView.image = image;
    
    self.readnumAndLikenumLabel.text = [NSString stringWithFormat:@"%@ 人已读 | %@ 人喜欢",newsModel.readnum_newest,newsModel.likenum_newest];

}

- (void)layoutSubviews
{
    //判断news.title 的长度，和titleLabel的长度比较
    //如果title的长度大于titleLabel的长度 隐藏contentLabel
    
    //获取news.title的长度
    CGFloat titleLength = [self.newsModel.title sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width;
    
    if (titleLength > self.titleLabel.frame.size.width)
    {
        self.contentLabel.hidden = true;
    }else{
        self.contentLabel.hidden = false;
    }
}


//自定义Cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //配图
        UIImageView *picImageView = [[UIImageView alloc]init];
        self.picImageView = picImageView;
        picImageView.layer.cornerRadius = 6;
        picImageView.layer.masksToBounds = YES;
        picImageView.contentMode = UIViewContentModeScaleAspectFill;
        picImageView.layer.shadowColor = [[UIColor lightGrayColor] CGColor];
        picImageView.layer.shadowOffset = CGSizeMake(1, 2);
        [self addSubview:picImageView];
        [picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.top.mas_equalTo(10);
            make.height.mas_equalTo(60);
            make.width.mas_equalTo(100);
        }];
        
        //标题
        UILabel *titleLabel = [[UILabel alloc]init];
        self.titleLabel = titleLabel;
        titleLabel.numberOfLines = 0;
        [self addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(8);
            make.left.equalTo(picImageView.mas_right).offset(10);
            make.right.equalTo(self).offset(-5);
//            make.height.mas_equalTo(10);
//            make.height.mas_equalTo(60);
        }];
        
        //摘要
        UILabel *contentLabel = [[UILabel alloc]init];
        self.contentLabel = contentLabel;
        contentLabel.font = [UIFont systemFontOfSize:fontSize];
        contentLabel.textColor = [UIColor lightGrayColor];
        contentLabel.numberOfLines = 0;
        [self addSubview:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(2);
            make.left.equalTo(titleLabel);
            make.right.equalTo(self).offset(-5);
            make.height.mas_equalTo(40);
        }];
        
        //阅读和喜欢的人数
        UILabel *readnumAndLikenumLabel = [[UILabel alloc]init];
        self.readnumAndLikenumLabel = readnumAndLikenumLabel;
        readnumAndLikenumLabel.font = [UIFont systemFontOfSize:fontSize];
        readnumAndLikenumLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:readnumAndLikenumLabel];
        [readnumAndLikenumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-12);
            make.left.equalTo(titleLabel);
        }];
        
        //发布平台的名称
        UILabel *nicknameLabel = [[UILabel alloc]init];
        self.nicknameLabel = nicknameLabel;
        nicknameLabel.font = [UIFont systemFontOfSize:fontSize];
        nicknameLabel.textColor = [UIColor linkColor];
        nicknameLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:nicknameLabel];
        [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
             make.bottom.mas_equalTo(-12);
            make.right.equalTo(self.mas_right).offset(-10);
        }];
    }
    return self;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
